package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.BluetoothCardDaoI;
import sy.model.BluetoothCard;
@Repository("bluetoothCardDao")
public class BluetoothCardDaoImpl implements BluetoothCardDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(BluetoothCard bluetoothCard) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(bluetoothCard);
	}

	@Override
	public List<BluetoothCard> list(String accountId) {
		// TODO Auto-generated method stub
		String hql = "from BluetoothCard where accountId = :accountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("accountId", accountId);
		List<BluetoothCard> list = query.list();
		return list;
	}

	@Override
	public void update(BluetoothCard bluetoothCard) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(bluetoothCard);
	}

	@Override
	public BluetoothCard getBluetoothCardByAddress(String address, String accountId) {
		// TODO Auto-generated method stub
		String hql = "from BluetoothCard where address = :address and accountId = :accountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("address", address);
		query.setString("accountId", accountId);
		List<BluetoothCard> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void deleteByAddress(String address,String accountId) {
		// TODO Auto-generated method stub
		String hql = "delete from BluetoothCard where address = :address and accountId = :accountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("address", address);
		query.setString("accountId", accountId);
		query.executeUpdate();
	}
}
